﻿using System;

namespace Simp.CodeModel.CoreFunctions
{
	/// <summary>Encapsulates the division of two expressions.</summary>
	/// <remarks>Simplifications can be done iif the left and right operand are numbers.</remarks>
	public class DivisionFunction : BinaryFunctionBase, IFunction
	{
		/// <summary>See <see cref="IFunction.FunctionName"/>.</summary>
		public override string FunctionName
		{
			get { return "Division"; }
		}

		/// <summary>Returns an division operation.</summary>
		/// <param name="a"/>
		/// <param name="b"/>
		/// <returns/>
		protected override IFunction IntegerOperation(int a, int b)
		{
			if (b == 0)
			{	//	Can't resolve a division by zero
				return null;
			}
			else
			{
				return RationalFunction.GetSimplified(a, b);
			}
		}

		/// <summary>Returns an division operation.</summary>
		/// <param name="a"/>
		/// <param name="b"/>
		/// <returns/>
		protected override IFunction RationalOperation(RationalFunction a, RationalFunction b)
		{
			if (b.Numerator == 0)
			{	//	Can't resolve a division by zero
				return null;
			}
			else
			{
				return RationalFunction.GetSimplified(a.Numerator * b.Denominator, a.Denominator * b.Numerator);
			}
		}

		/// <summary>Returns a division operation.</summary>
		/// <param name="a"/>
		/// <param name="b"/>
		/// <returns/>
		protected override IFunction RealOperation(double a, double b)
		{
			if (b == 0)
			{	//	Can't resolve a division by zero
				return null;
			}
			else
			{
				return new RealFunction(a / b);
			}
		}

		/// <summary>Returns the symbol of the operation.</summary>
		protected override string OperationSymbol
		{
			get { return "/"; }
		}
	}
}